/* main.c */
#include "app_inc.h"

NRF24L01_Config_T config =
{
    {0x34, 0x43, 0x10, 0x10, 0x01}, /* TxAddr. */
    {0x34, 0x43, 0x10, 0x10, 0x01}  /* RxAddr. */
};

extern RBUF_Handler_T gDrvUartTxBufStruct;
extern RBUF_Handler_T gDrvUartRxBufStruct;
uint8_t gRfXferBuff[NRF24L01_XFER_PACKAGE_LENGTH];

int main(void)
{
    uint32_t i;

    BSP_InitStdioUART(115200U);
    DRV_UART_InitAddWithIntMode();

    /* printf("WirelessUart Demo ...\r\n"); */

    if (!NRF24L01_Init(&config))
    {
        printf("#Error# NRF24L01 is not detected.\r\n");
        return 0; /* Leave the application. */
    }

    while (1)
    {
        /* RF Rx Process. */
        if (NRF24L01_GetRxPackageReadyFlag())
        {
            NRF24L01_GetRxPackage(gRfXferBuff);
            NRF24L01_ClearRxPackageReadyFlag();
            
            /* Move the data to Uart Tx Buffer. */
            for (i = 0U; i < gRfXferBuff[0]; i++)
            {
                if (!RBUF_IsFull(&gDrvUartTxBufStruct))
                {
                    RBUF_PutDataIn(&gDrvUartTxBufStruct, gRfXferBuff[1U+i]);
                }
            }
            /* Enable the Uart Tx interrupt to transfer the data. */
            if (!DRV_UART_IsTxInterruptEnabled())
            {
                DRV_UART_EnableTxInterrupt(true);
            }
        }

        /* RF Tx Process. */
        i = 0U;
        while (   (!RBUF_IsEmpty(&gDrvUartRxBufStruct))
               && (i < (NRF24L01_XFER_PACKAGE_LENGTH-1U))  )
        {
            gRfXferBuff[1U+i] = RBUF_GetDataOut(&gDrvUartRxBufStruct);
            i++;
        }

        if (i != 0U)
        {
            gRfXferBuff[0U] = i;
            NRF24L01_TxPackage(gRfXferBuff);
        }
    }
}

/* EOF. */
